// ==UserScript==
// @name           CruiseControl Build Results
// @namespace      http://www.agfa.com
// @description    Make the build results pages more useable
// @include        *cruisecontrol/buildresults*
// ==/UserScript==
var hideByDefault = true;

function showHideContents(event){

	if (event.currentTarget.innerHTML.indexOf('Error') >= 0)
	{
    		var bottomTable = document.getElementById('bottomTable');
    		
    		( bottomTable.style.display == '' ) ? bottomTable.style.display = 'none' : bottomTable.style.display = '';

	}
	else
	{
		var topTable = grabTopTable();
		
		if (topTable != null)
		    ( topTable.style.display == '' ) ? topTable.style.display = 'none' : topTable.style.display = '';
	}
}

function grabTopTable(){

	var mainTableInfo = document.evaluate(
    "//table/tbody/tr/td[@class='unittests-data']",
    document,
    null,
    XPathResult.ANY_TYPE,
    null);

    var tableInfo = mainTableInfo.iterateNext();

    if ( tableInfo.innerHTML.indexOf('All Tests Passed') >= 0 ) return null;

    var mainTable = tableInfo.parentNode.parentNode;

    return mainTable;
}

function prepareLowerTable(){
	
	var mainTableInfo = document.evaluate(
    "//table/tbody/tr/td[@class='unittests-sectionheader']",
    document,
    null,
    XPathResult.ANY_TYPE,
    null);

    var tableInfo = mainTableInfo.iterateNext();
    var mainTable = tableInfo.parentNode.parentNode;
    var firstRowFound = false;
    var x = 0;
    var tempArray;
    var groupTable = document.createElement('table');
    groupTable.setAttribute('width', '98%');
    groupTable.setAttribute('border', '0');
    groupTable.setAttribute('cellspacing', '0');
    groupTable.setAttribute('cellpadding', '2');
    groupTable.setAttribute('align', 'center');
    groupTable.setAttribute('id', 'bottomTable');
    var tBody = document.createElement('tbody');
    groupTable.appendChild(tBody);

    for(var i = 0; i < mainTable.childNodes.length; i++)
    {
    	var currentRow = mainTable.childNodes[i];
    	
    	if (firstRowFound)
    	{
    		tempArray[x] = currentRow;
    		x++;
    	}
	else
    	{
    		if (currentRow.innerHTML != null && currentRow.innerHTML.indexOf('Unit Test Error Details') >= 0)
    		{
    			firstRowFound = true;
    			tempArray = new Array(mainTable.childNodes.length - i - 1);
    			var number = mainTable.childNodes.length - i - 1;
    		}
	}
    }
    
    if (tempArray != null)
    {
    	for( var j = 0; j < tempArray.length; j++ )
    	{
    	    tBody.appendChild(tempArray[j]);
    	}
    }

    var newCell = document.createElement('td');
    newCell.appendChild(groupTable);
    var newRow = document.createElement('tr');
    newRow.appendChild(newCell);
    
    mainTable.appendChild(newRow);

}

function addEventHandlers(){

    var tds = document.getElementsByTagName( 'TD' );
    for( var j = 0; j < tds.length; j++ )
    {
	if (tds[j].className == 'unittests-sectionheader')
	{
		var tr = tds[j].parentNode;
		tr.className = 'unitTestSection' + j;
		var testDetails = grabTopTable();
		if (testDetails != null)
		{
		    tr.style.cursor = 'pointer';
		    tr.addEventListener('click', showHideContents, true);
		}
	}
    }

}

function showHideTables(){

    var topTable = grabTopTable();
    var bottomTable = document.getElementById('bottomTable');

    if ( hideByDefault )
    {
	if (topTable != null) topTable.style.display = 'none';
	if (bottomTable != null) bottomTable.style.display = 'none';
    }
    else
    {
    	if (topTable != null) topTable.style.display = '';
    	if (bottomTable != null) bottomTable.style.display = '';
    }
}

function issueLinking(){
	var misRE = /(^|[^0-9])([0-9]{5,6})([^0-9]|$)/i;
	
	var modificationTableInfo = document.evaluate(
	"//table/tbody/tr/td[@class='modifications-sectionheader']",
    	document,
    	null,
    	XPathResult.ANY_TYPE,
    	null);

	var tableInfo = modificationTableInfo.iterateNext();
    	var modificationTable = tableInfo.parentNode.parentNode;
	
	var tds = modificationTable.getElementsByTagName('td');
	
	for( var i = 0; i < tds.length; i++ )
	{
		if ( tds[i].className == "modifications-data" )
		{
			var cellContents = tds[i].innerHTML;
			var matches = cellContents.match(misRE);

			if( matches != null )
			{
				tds[i].innerHTML = cellContents.replace(misRE, function (str, p1, p2, p3, offset, s) {
					return p1 + p2.link("https://mist.mitra.com/MISCore/importsissue/Issue.asp?Ref=" + p2) + p3; });
			}
		}
	}
	
	// This section is to highlight the MIS Issue # for the Last Log Entry in the header information
	var lastLogEntryInfo = document.evaluate(
	"//table/tbody/tr/td[@class='header-title']",
	document,
	null,
	XPathResult.ANY_TYPE,
	null);
	
	var headerTableInfo = lastLogEntryInfo.iterateNext();
	var headerTable = headerTableInfo.parentNode.parentNode;
	
	var headerTds = headerTable.getElementsByTagName('td');
	
	for( var j = 0; j < headerTds.length; j++ )
	{
		if ( headerTds[j].className == "header-data" )
		{
			var tdContents = headerTds[j].innerHTML
			
			if ( tdContents.indexOf('Last log entry:') >= 0 )
			{
				var issueMatches = tdContents.match(misRE);
				
				if ( issueMatches != null )
				{
					headerTds[j].innerHTML = tdContents.replace(misRE, function (str, p1, p2, p3, offset, s) {
					return p1 + p2.link("https://mist.mitra.com/MISCore/importsissue/Issue.asp?Ref=" + p2) + p3; });
				}
			}
		}
	}
}

function runScriptMain(){
	addEventHandlers();
	prepareLowerTable();	
	showHideTables();
	issueLinking();
}

var go = runScriptMain();
